/*
* Inline Form Validation Engine 1.6, jQuery plugin
* 
* Copyright(c) 2009, Cedric Dugas
* http://www.position-relative.net
*	
* Form validation engine which allow custom regex rules to be added.
* Thanks to Francois Duquette
* Licenced under the MIT Licence
*/


(function($) {

    var firstvalid = true;
    $.fn.validationEngine = function(settings) {


        if ($.validationEngineLanguage) {				// IS THERE A LANGUAGE LOCALISATION ?
            allRules = $.validationEngineLanguage.allRules
        } else {
            allRules = { "required": {
                "regex": "none",
                "alertText": "请输入相关内容",
                "alertTextCheckboxMultiple": "请选择",
                "alertTextCheckboxe": "* 请选择"
            }, "focushtml": {
                "regex": "none",
                "alertText": "3-5个字符",
                "alertTextCheckboxMultiple": "请选择",
                "alertTextCheckboxe": "* 请选择"
            },
                "length": {
                    "regex": "none",
                    "alertText": "",
                    "alertText2": "-",
                    "alertText3": "个字符,包括字母、数字、下划线"
                },
                "maxCheckbox": {
                    "regex": "none",
                    "alertText": "* Checks allowed Exceeded"
                },
                "minCheckbox": {
                    "regex": "none",
                    "alertText": "* Please select ",
                    "alertText2": " options"
                },
                "confirm": {
                    "regex": "none",
                    "alertText": "确认密码与登录密码不一致!"
                },
                "telephone": {
                    "regex": "^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$",
                    "alertText": "请输入有效的电话号码"
                },
                "DH": {
                    "regex": "^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$",
                    "alertText": "请输入有效的手机号码"
                },
                "email": {
                    "regex": "/^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/",
                    "alertText": "您的邮件地址不正确"
                },
                "date": {
                    "regex": "/^[0-9]{4}\-\[0-9]{1,2}\-\[0-9]{1,2}$/",
                    "alertText": "Invalid date, must be in YYYY-MM-DD format"
                },
                "onlyNumber": {
                    "regex": "/^[0-9\ ]+$/",
                    "alertText": "* 请输入整数"
                },
                "noSpecialCaracters": {
                    "regex": "/^[0-9a-zA-Z]+$/",
                    "alertText": "* No special caracters allowed"
                },
                "ajaxUser": {
                    "file": "validateUser.php",
                    "alertTextOk": "* This user is available",
                    "alertTextLoad": "* Loading, please wait",
                    "alertText": "* This user is already taken"
                },
                "ajaxName": {
                    "file": "validateUser.php",
                    "alertText": "* This name is already taken",
                    "alertTextOk": "* This name is available",
                    "alertTextLoad": "* Loading, please wait"
                },
                "onlyLetter": {
                    "regex": "/^[a-zA-Z\ \']+$/",
                    "alertText": "* Letters only"
                },
                "NumMax": {
                    "regex": "/^[0-9\ ]+$/",
                    "alertText1": "* 请输入",
                    "alertText2": " 到 ",
                    "alertText3": " 的数值 "
                },
                "HanZi": {
                    "alertText": " 请输入汉字",
                    "alertText1": " 请输入长度为",
                    "alertText2": " 到 ",
                    "alertText3": " 的汉字"
                },
                "SelNo":
					{
					    "regex": "/^[a-zA-Z\ \']+$/",
					    "alertText": "请选择一个有效资料"
					},
                "CheckPhone":
					{
					    "regex": "none",
					    "alertText": "请以正确的电话格式填写"
					}
            }
        }
        settings = jQuery.extend({
            No: false,
            allrules: allRules,
            validationEventTriggers: "blur",
            validationEventTriggers1: "keyup",
            validationEventTriggersF: "focus",
            inlineValidation: true,
            returnIsValid: false,
            ajaxSubmit: false,
            promptPosition: "topRight", // OPENNING BOX POSITION, IMPLEMENTED: topLeft, topRight, bottomLeft, centerRight, bottomRight
            success: false,
            failure: function() { }
        }, settings);

        $.validationEngine.ajaxValidArray = new Array()	// ARRAY FOR AJAX: VALIDATION MEMORY 

        if (settings.inlineValidation == true) { 		// Validating Inline ?
            if (firstvalid == true) {					// NEEDED FOR THE SETTING returnIsValid
                $(this).find("[class^=validate]").not("[type=checkbox]").bind(settings.validationEventTriggers, function(caller) { _inlinEvent(this) })
                if (settings.validationEventTriggers1 != "") {
                    $(this).find("[class^=validate]").not("[type=checkbox]").bind(settings.validationEventTriggers1, function(caller) { _inlinEvent(this) })
                }
                $(this).find("[class^=validate]").not("[type=checkbox]").bind(settings.validationEventTriggersF, function(caller) { _inlinEvent(this) })
                $(this).find("[class^=validate][type=checkbox]").bind("click", function(caller) { _inlinEvent(this) })
                firstvalid = false;
            }
            function _inlinEvent(caller) {
                if ($.validationEngine.intercept == false || !$.validationEngine.intercept) {		// STOP INLINE VALIDATION THIS TIME ONLY
                    $.validationEngine.onSubmitValid = false;
                    $.validationEngine.loadValidation(caller, settings);
                } else {
                    $.validationEngine.intercept = false;
                }
            }
        }

        if (settings.returnIsValid == true) {		// Do validation and return true or false, it bypass everything;

            if ($.validationEngine.submitValidation(this, settings)) {
                return false;
            } else {
                return true;
            }
        }

        $(this).bind("submit", function(caller) {   // ON FORM SUBMIT, CONTROL AJAX FUNCTION IF SPECIFIED ON DOCUMENT READY
            $.validationEngine.onSubmitValid = true;
            if ($.validationEngine.submitValidation(this, settings) == false) {
                if ($.validationEngine.submitForm(this, settings) == true) { return false; }
            } else {
                settings.failure && settings.failure();
                return false;
            }
        })
    };
    $.validationEngine = {
        submitForm: function(caller) {
            if ($.validationEngine.settings.ajaxSubmit) {

                $.ajax({
                    type: "POST",
                    url: $.validationEngine.settings.ajaxSubmitFile,
                    async: true,
                    data: $(caller).serialize(),
                    error: function(data, transport) { $.validationEngine.debug("error in the ajax: " + data.status + " " + transport) },
                    success: function(data) {
                        if (data == "true") {			// EVERYTING IS FINE, SHOW SUCCESS MESSAGE
                            $(caller).css("opacity", 1)
                            $(caller).animate({ opacity: 0, height: 0 }, function() {
                                $(caller).css("display", "none")
                                $(caller).before("<div class='ajaxSubmit'>" + $.validationEngine.settings.ajaxSubmitMessage + "</div>")
                                $.validationEngine.closePrompt(".formError", true)
                                $(".ajaxSubmit").show("slow")
                                if ($.validationEngine.settings.success) {	// AJAX SUCCESS, STOP THE LOCATION UPDATE
                                    $.validationEngine.settings.success && $.validationEngine.settings.success();
                                    return false;
                                }
                            })
                        } else {						// HOUSTON WE GOT A PROBLEM (SOMETING IS NOT VALIDATING)
                            data = eval("(" + data + ")");
                            if (!data.jsonValidateReturn) {
                                $.validationEngine.debug("you are not going into the success fonction and jsonValidateReturn return nothing")
                            }
                            errorNumber = data.jsonValidateReturn.length
                            for (index = 0; index < errorNumber; index++) {
                                fieldId = data.jsonValidateReturn[index][0];
                                promptError = data.jsonValidateReturn[index][1];
                                type = data.jsonValidateReturn[index][2];
                                $.validationEngine.buildPrompt(fieldId, promptError, type);
                            }
                        }
                    }
                })
                return true;
            }
            if ($.validationEngine.settings.success) {	// AJAX SUCCESS, STOP THE LOCATION UPDATE
                $(caller).unbind("submit")
                $.validationEngine.settings.success && $.validationEngine.settings.success();
                return true;
            }
            return false;
        },
        buildPrompt: function(caller, promptText, type, ajaxed) {			// ERROR PROMPT CREATION AND DISPLAY WHEN AN ERROR OCCUR
            //错误提示div创建
            var divFormError = document.createElement('div');
            var formErrorContent = document.createElement('div');

            linkTofield = $(caller).attr("id") + "formError";
            linkTofield = linkTofield.replace("[", ""); linkTofield = linkTofield.replace("]", "");

            $(divFormError).addClass("formError")

            if (type == "pass") { $(divFormError).addClass("greenPopup") }
            if (type == "load") { $(divFormError).addClass("blackPopup") }
            if (ajaxed) { $(divFormError).addClass("ajaxed") }

            $(divFormError).addClass(linkTofield)
            $(formErrorContent).addClass("formErrorContent")

            $("body").append(divFormError)
            $(divFormError).append(formErrorContent)

            if ($.validationEngine.showTriangle != false) {		// NO TRIANGLE ON MAX CHECKBOX AND RADIO
                var arrow = document.createElement('div')
                $(arrow).addClass("formErrorArrow")
                $(divFormError).append(arrow)
                if ($.validationEngine.settings.promptPosition == "bottomLeft" || $.validationEngine.settings.promptPosition == "bottomRight") {
                    $(arrow).addClass("formErrorArrowBottom")
                    $(arrow).html('<div class="line1"><!-- --></div><div class="line2"><!-- --></div><div class="line3"><!-- --></div><div class="line4"><!-- --></div><div class="line5"><!-- --></div><div class="line6"><!-- --></div><div class="line7"><!-- --></div><div class="line8"><!-- --></div><div class="line9"><!-- --></div><div class="line10"><!-- --></div>');
                }
                if ($.validationEngine.settings.promptPosition == "topLeft" || $.validationEngine.settings.promptPosition == "topRight") {
                    $(divFormError).append(arrow)
                    $(arrow).html('<div class="line10"><!-- --></div><div class="line9"><!-- --></div><div class="line8"><!-- --></div><div class="line7"><!-- --></div><div class="line6"><!-- --></div><div class="line5"><!-- --></div><div class="line4"><!-- --></div><div class="line3"><!-- --></div><div class="line2"><!-- --></div><div class="line1"><!-- --></div>');
                }
            }

            $(formErrorContent).html(promptText)

            callerTopPosition = $(caller).offset().top;
            callerleftPosition = $(caller).offset().left;
            callerWidth = $(caller).width()
            inputHeight = $(divFormError).height()

            /* POSITIONNING */
            if ($.validationEngine.settings.promptPosition == "topRight") { callerleftPosition += callerWidth - 30; callerTopPosition += -inputHeight - 10; }
            if ($.validationEngine.settings.promptPosition == "topLeft") { callerTopPosition += -inputHeight - 10; }

            if ($.validationEngine.settings.promptPosition == "centerRight") { callerleftPosition += callerWidth + 13; }

            if ($.validationEngine.settings.promptPosition == "bottomLeft") {
                callerHeight = $(caller).height();
                callerleftPosition = callerleftPosition;
                callerTopPosition = callerTopPosition + callerHeight + 15;
            }
            if ($.validationEngine.settings.promptPosition == "bottomRight") {
                callerHeight = $(caller).height();
                callerleftPosition += callerWidth - 30;
                callerTopPosition += callerHeight + 15;
            }
            $(divFormError).css({
                top: callerTopPosition,
                left: callerleftPosition,
                opacity: 0
            })
            return $(divFormError).animate({ "opacity": 0.87 }, function() {
                var strID = $(caller).attr("id") + "hgwID";
                $("#" + strID).remove();
                return true;
            });
        },
        updatePromptText: function(caller, promptText, type, ajaxed) {	// UPDATE TEXT ERROR IF AN ERROR IS ALREADY DISPLAYED

            linkTofield = $(caller).attr("id") + "formError";
            linkTofield = linkTofield.replace("[", ""); linkTofield = linkTofield.replace("]", "");

            var updateThisPrompt = "." + linkTofield;

            if (type == "pass") { $(updateThisPrompt).addClass("greenPopup") } else { $(updateThisPrompt).removeClass("greenPopup") };
            if (type == "load") { $(updateThisPrompt).addClass("blackPopup") } else { $(updateThisPrompt).removeClass("blackPopup") };
            if (ajaxed) { $(updateThisPrompt).addClass("ajaxed") } else { $(updateThisPrompt).removeClass("ajaxed") };

            $(updateThisPrompt).find(".formErrorContent").html(promptText)
            callerTopPosition = $(caller).offset().top;
            inputHeight = $(updateThisPrompt).height()

            if ($.validationEngine.settings.promptPosition == "bottomLeft" || $.validationEngine.settings.promptPosition == "bottomRight") {
                callerHeight = $(caller).height()
                callerTopPosition = callerTopPosition + callerHeight + 15
            }
            if ($.validationEngine.settings.promptPosition == "centerRight") { callerleftPosition += callerWidth + 13; }
            if ($.validationEngine.settings.promptPosition == "topLeft" || $.validationEngine.settings.promptPosition == "topRight") {
                callerTopPosition = callerTopPosition - inputHeight - 10
            }
            $(updateThisPrompt).animate({ top: callerTopPosition });
        },
        loadValidation: function(caller, settings) {		// GET VALIDATIONS TO BE EXECUTED

            $.validationEngine.settings = settings
            rulesParsing = $(caller).attr('class');
            rulesRegExp = /\[(.*)\]/;
            getRules = rulesRegExp.exec(rulesParsing);
            str = getRules[1]
            pattern = /\W+/;
            result = str.split(pattern);

            var validateCalll = $.validationEngine.validateCall(caller, result)
            return validateCalll
        },
        validateCall: function(caller, rules) {	// EXECUTE VALIDATION REQUIRED BY THE USER FOR THIS FIELD
            var promptText = ""

            if (!$(caller).attr("id")) { $.validationEngine.debug("This field have no ID attribut( name & class displayed): " + $(caller).attr("name") + " " + $(caller).attr("class")) }

            var prompt = $(caller).attr("id");
            var caller = caller;
            ajaxValidate = false
            var callerName = $(caller).attr("name")
            $.validationEngine.isError = false;
            $.validationEngine.showTriangle = true
            callerType = $(caller).attr("type");
            promptText = "";
            for (i = 0; i < rules.length; i++) {
                switch (rules[i]) {
                    case "optional":
                        if (!$(caller).val()) {
                            $.validationEngine.closePrompt(caller)
                            return $.validationEngine.isError
                        }
                        break;
                    case "required":
                        _required(caller, rules);
                        break;
                    case "focushtml":
                        _focushtml(caller, rules, i);
                        break;
                    case "custom":
                        _customRegex(caller, rules, i);
                        break;
                    case "ajax":
                        if (!$.validationEngine.onSubmitValid) {
                            _ajax(caller, rules, i);
                        }
                        break;
                    case "ajaxFunc":
                        _ajaxFunc(caller, rules, i);
                        break;
                    case "length":
                        _length(caller, rules, i);
                        break;
                    case "lengthMsg":
                        _lengthMSG(caller, rules, i);
                        break;
                    case "maxCheckbox":
                        _maxCheckbox(caller, rules, i);
                        groupname = $(caller).attr("name");
                        caller = $("input[name='" + groupname + "']")
                        break;
                    case "minCheckbox":
                        _minCheckbox(caller, rules, i);
                        groupname = $(caller).attr("name");
                        caller = $("input[name='" + groupname + "']")
                        break;
                    case "confirm":
                        _confirm(caller, rules, i);
                        break;
                    case "NumMax":
                        _NumMax(caller, rules, i);
                        break;
                    case "HanZi":
                        _Hanzi(caller, rules, i);
                    case "SelNo":
                        _SelNo(caller, rules, i);
                        break;
                    case "CheckPhone":
                        _CheckPhone(caller, rules, i);
                        break;
                    case "DH":
                        _DH(caller, rules, i);
                        break;
                    case "email":
                        _email(caller, rules, i);
                        break;
                    default: ;
                };
                if (promptText != "") {
                    break;
                }
            };


            if ($.validationEngine.isError == true) {
                radioHackOpen();
                ($("div." + prompt + "formError").size() == 0) ? $.validationEngine.buildPrompt(caller, promptText, "error") : $.validationEngine.updatePromptText(caller, promptText);
            } else {
                radioHackClose();
                $.validationEngine.closePrompt(caller);
            }
            /* UNFORTUNATE RADIO AND CHECKBOX GROUP HACKS */
            /* As my validation is looping input with id's we need a hack for my validation to understand to group these inputs */
            function radioHackOpen() {
                if ($("input[name='" + callerName + "']").size() > 1 && (callerType == "radio" || callerType == "checkbox")) {        // Hack for radio/checkbox group button, the validation go the first radio/checkbox of the group
                    caller = $("input[name='" + callerName + "'][type!=hidden]:first");
                    $.validationEngine.showTriangle = false;
                    prompt = $(caller).attr("id");
                }
            }
            function radioHackClose() {
                if ($("input[name='" + callerName + "']").size() > 1 && (callerType == "radio" || callerType == "checkbox")) {       //  Hack for radio/checkbox group button, the validation go the first radio/checkbox of the group
                    caller = $("input[name='" + callerName + "'][type!=hidden]:first");
                }
            }
            /* VALIDATION FUNCTIONS */
            function _required(caller, rules) {   // VALIDATE BLANK FIELD
                callerType = $(caller).attr("type");
                var tempStr = $(caller).attr("validatemsg") || "";
                if (tempStr.length > 0) {
                    $.validationEngine.settings.allrules[rules[i]].alertText = tempStr;

                }

                if (callerType == "text" || callerType == "password" || callerType == "textarea") {

                    if (!$(caller).val()) {
                        $.validationEngine.isError = true;
                        promptText += $.validationEngine.settings.allrules[rules[i]].alertText + "<br />";
                    }
                }
                if (callerType == "radio" || callerType == "checkbox") {
                    callerName = $(caller).attr("name");

                    if ($("input[name='" + callerName + "']:checked").size() == 0) {
                        $.validationEngine.isError = true;
                        if ($("input[name='" + callerName + "']").size() == 1) {
                            promptText += $.validationEngine.settings.allrules[rules[i]].alertTextCheckboxe + "<br />";
                        } else {
                            promptText += $.validationEngine.settings.allrules[rules[i]].alertTextCheckboxMultiple + "<br />";
                        }
                    }
                }
                if (callerType == "select-one") { // added by paul@kinetek.net for select boxes, Thank you
                    callerName = $(caller).attr("id");

                    if (!$("#" + callerName).val()) {
                        $.validationEngine.isError = true;
                        promptText += $.validationEngine.settings.allrules[rules[i]].alertText + "<br />";
                    }
                }
                if (callerType == "select-multiple") { // added by paul@kinetek.net for select boxes, Thank you
                    callerName = $(caller).attr("id");

                    if (!$("#" + callerName).val()) {
                        $.validationEngine.isError = true;
                        promptText += $.validationEngine.settings.allrules[rules[i]].alertText + "<br />";
                    }
                }
            }
            function _customRegex(caller, rules, position) {		 // VALIDATE REGEX RULES
                customRule = rules[position + 1];
                pattern = eval($.validationEngine.settings.allrules[customRule].regex);

                if (!pattern.test($(caller).attr('value'))) {
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules[customRule].alertText + "<br />";
                }
            }
            function _ajax(caller, rules, position) {				 // VALIDATE AJAX RULES

                customAjaxRule = rules[position + 1];
                postfile = $.validationEngine.settings.allrules[customAjaxRule].file;
                fieldValue = $(caller).val();
                ajaxCaller = caller;
                fieldId = $(caller).attr("id");
                ajaxValidate = true;
                ajaxisError = $.validationEngine.isError;

                /* AJAX VALIDATION HAS ITS OWN UPDATE AND BUILD UNLIKE OTHER RULES */
                if (!ajaxisError) {
                    $.ajax({
                        type: "POST",
                        url: postfile,
                        async: true,
                        data: "validateValue=" + fieldValue + "&validateId=" + fieldId + "&validateError=" + customAjaxRule,
                        beforeSend: function() {		// BUILD A LOADING PROMPT IF LOAD TEXT EXIST		   			
                            if ($.validationEngine.settings.allrules[customAjaxRule].alertTextLoad) {

                                if (!$("div." + fieldId + "formError")[0]) {
                                    return $.validationEngine.buildPrompt(ajaxCaller, $.validationEngine.settings.allrules[customAjaxRule].alertTextLoad, "load");
                                } else {
                                    $.validationEngine.updatePromptText(ajaxCaller, $.validationEngine.settings.allrules[customAjaxRule].alertTextLoad, "load");
                                }
                            }
                        },
                        error: function(data, transport) { $.validationEngine.debug("error in the ajax: " + data.status + " " + transport) },
                        success: function(data) {					// GET SUCCESS DATA RETURN JSON
                            data = eval("(" + data + ")"); 			// GET JSON DATA FROM PHP AND PARSE IT
                            ajaxisError = data.jsonValidateReturn[2];
                            customAjaxRule = data.jsonValidateReturn[1];
                            ajaxCaller = $("#" + data.jsonValidateReturn[0])[0];
                            fieldId = ajaxCaller;
                            ajaxErrorLength = $.validationEngine.ajaxValidArray.length
                            existInarray = false;

                            if (ajaxisError == "false") {			// DATA FALSE UPDATE PROMPT WITH ERROR;

                                _checkInArray(false)				// Check if ajax validation alreay used on this field

                                if (!existInarray) {		 			// Add ajax error to stop submit		 		
                                    $.validationEngine.ajaxValidArray[ajaxErrorLength] = new Array(2)
                                    $.validationEngine.ajaxValidArray[ajaxErrorLength][0] = fieldId
                                    $.validationEngine.ajaxValidArray[ajaxErrorLength][1] = false
                                    existInarray = false;
                                }

                                $.validationEngine.ajaxValid = false;
                                promptText += $.validationEngine.settings.allrules[customAjaxRule].alertText + "<br />";
                                $.validationEngine.updatePromptText(ajaxCaller, promptText, "", true);
                            } else {
                                _checkInArray(true)

                                $.validationEngine.ajaxValid = true;
                                if ($.validationEngine.settings.allrules[customAjaxRule].alertTextOk) {	// NO OK TEXT MEAN CLOSE PROMPT	 			
                                    $.validationEngine.updatePromptText(ajaxCaller, $.validationEngine.settings.allrules[customAjaxRule].alertTextOk, "pass", true);
                                } else {
                                    ajaxValidate = false;
                                    $.validationEngine.closePrompt(ajaxCaller);
                                }
                            }
                            function _checkInArray(validate) {
                                for (x = 0; x < ajaxErrorLength; x++) {
                                    if ($.validationEngine.ajaxValidArray[x][0] == fieldId) {
                                        $.validationEngine.ajaxValidArray[x][1] = validate
                                        existInarray = true;

                                    }
                                }
                            }
                        }
                    });
                }
            }
            function _confirm(caller, rules, position) {		 // VALIDATE FIELD MATCH
                confirmField = rules[position + 1];

                if ($(caller).attr('value') != $("#" + confirmField).attr('value')) {
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules["confirm"].alertText + "<br />";
                }
            }

            function _NumMax(caller, rules, position) {
                startLength = eval(rules[position + 1]);
                endLength = eval(rules[position + 2]);
                feildLength = $(caller).attr('value');

                if (isNaN(parseInt(feildLength))) {
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules["onlyNumber"].alertText + "<br />"

                } else {
                    if (feildLength != feildLength.replace(".", "")) {
                        $.validationEngine.isError = true;
                        promptText += $.validationEngine.settings.allrules["onlyNumber"].alertText + "<br />"
                    }
                }
                if (feildLength < startLength || feildLength > endLength) {
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules["NumMax"].alertText1 + startLength + $.validationEngine.settings.allrules["NumMax"].alertText2 + endLength + $.validationEngine.settings.allrules["NumMax"].alertText3 + "<br />"
                }
            }

            function _ajaxFunc(caller, rules, position) {

                var funcName = rules[position + 1] + "()";
                var funcValue = eval(funcName);
                if (funcValue.length > 0) {
                    promptText += funcValue;
                    $.validationEngine.isError = true;
                } else {
                }


            }
            function _SelNo(caller, rules, position) {

                var strMR = eval(rules[position + 1]);
                feildLength = $(caller).attr('value');
                if (strMR == feildLength) {
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules["SelNo"].alertText + "<br />"
                }
            }
            function _DH(caller, rules, position) {

                var parten1 = /^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$/;
                var strNR = $(caller).attr('value') || "";
                strNR = $.trim(strNR);
                if (strNR == "") {
                    return;
                }
                if (!(parten1.test($(caller).attr('value')))) {
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules["DH"].alertText + "<br />";
                }
            }
            function _email(caller, rules, position) {

                var parten1 = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/;
                var strNR = $(caller).attr('value') || "";
                strNR = $.trim(strNR);
                if (strNR == "") {
                    return;
                }
                if (!(parten1.test($(caller).attr('value')))) {
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules["email"].alertText + "<br />";
                }
            }
            function _Hanzi(caller, rules, position) {

                var parten1 = /^[\u4E00-\u9FA5]+$/;

                if (!(parten1.test($(caller).attr('value')))) {
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules["HanZi"].alertText + "<br />";
                } else {
                    startLength = eval(rules[position + 1]);
                    endLength = eval(rules[position + 2]);
                    feildLength = $(caller).attr('value').length;

                    if (feildLength < startLength || feildLength > endLength) {
                        $.validationEngine.isError = true;
                        promptText += $.validationEngine.settings.allrules["HanZi"].alertText1 + startLength + $.validationEngine.settings.allrules["HanZi"].alertText2 + endLength + $.validationEngine.settings.allrules["HanZi"].alertText3 + "<br />"
                    }

                }
            }
            function _focushtml(caller, rules, position) {

                //$.validationEngine.isError = true;

                //alert(eval(rules[position+1])+"你好");

                //                var parten1 = /^[\u4E00-\u9FA5]+$/;

                //                if (!(parten1.test($(caller).attr('value')))) {
                //                    $.validationEngine.isError = true;
                //                    promptText += $.validationEngine.settings.allrules["HanZi"].alertText + "<br />";
                //                } else {
                //                    startLength = eval(rules[position + 1]);
                //                    endLength = eval(rules[position + 2]);
                //                    feildLength = $(caller).attr('value').length;

                //                    if (feildLength < startLength || feildLength > endLength) {
                //                        $.validationEngine.isError = true;
                //                        promptText += $.validationEngine.settings.allrules["HanZi"].alertText1 + startLength + $.validationEngine.settings.allrules["HanZi"].alertText2 + endLength + $.validationEngine.settings.allrules["HanZi"].alertText3 + "<br />"
                //                    }

                //                }
            }
            function _CheckPhone(caller, rules, position) {
                var parten1 = /^(0[\d]{2,3}-)?\d{6,8}(-\d{3,4})?$/;
                var parten2 = /^0{0,1}(13[4-9]|15[7-9]|15[0-2]|18[7-8])[0-9]{8}$/;
                if (!(parten1.test($(caller).attr('value')) || parten2.test($(caller).attr('value')))) {
                    //alert(2);
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules["CheckPhone"].alertText + "<br />";
                }
            }

            function _length(caller, rules, position) {    	  // VALIDATE LENGTH

                startLength = eval(rules[position + 1]);
                endLength = eval(rules[position + 2]);
                feildLength = $(caller).attr('value').length;

                if (feildLength < startLength || feildLength > endLength) {
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules["length"].alertText + startLength + $.validationEngine.settings.allrules["length"].alertText2 + endLength + $.validationEngine.settings.allrules["length"].alertText3 + "<br />"
                }
            }
            function _lengthMSG(caller, rules, position) {    	  // VALIDATE LENGTH

                startLength = eval(rules[position + 1]);
                endLength = eval(rules[position + 2]);
                feildLength = $(caller).attr('value').length;

                if (feildLength < startLength || feildLength > endLength) {
                    $.validationEngine.isError = true;
                    promptText += $(caller).attr("lengthMsg") + "<br />"
                }
            }
            function _maxCheckbox(caller, rules, position) {  	  // VALIDATE CHECKBOX NUMBER

                nbCheck = eval(rules[position + 1]);
                groupname = $(caller).attr("name");
                groupSize = $("input[name='" + groupname + "']:checked").size();
                if (groupSize > nbCheck) {
                    $.validationEngine.showTriangle = false
                    $.validationEngine.isError = true;
                    promptText += $.validationEngine.settings.allrules["maxCheckbox"].alertText + "<br />";
                }
            }
            function _minCheckbox(caller, rules, position) {  	  // VALIDATE CHECKBOX NUMBER

                nbCheck = eval(rules[position + 1]);
                groupname = $(caller).attr("name");
                groupSize = $("input[name='" + groupname + "']:checked").size();
                if (groupSize < nbCheck) {

                    $.validationEngine.isError = true;
                    $.validationEngine.showTriangle = false;
                    promptText += $.validationEngine.settings.allrules["minCheckbox"].alertText + " " + nbCheck + " " + $.validationEngine.settings.allrules["minCheckbox"].alertText2 + "<br />";
                }
            }
            return ($.validationEngine.isError) ? $.validationEngine.isError : false;
        },
        closePrompt: function(caller, outside) {						// CLOSE PROMPT WHEN ERROR CORRECTED 修改正确后，关闭错误提示

            if (outside) {
                $(caller).fadeTo("fast", 0, function() {
                    $(caller).remove();
                });
                //$(closingPrompt).text("输入完成!");

                return false;
            }
            if (!ajaxValidate) {
                linkTofield = $(caller).attr("id") + "formError";
                linkTofield = linkTofield.replace("[", ""); linkTofield = linkTofield.replace("]", "");
                closingPrompt = "." + linkTofield;
                var styleStr = $(closingPrompt).attr("style");
                var strID = $(caller).attr("id") + "hgwID";
                //var strdiv = "<div class='formError' id='" + strID + "'  ><div class='formCorrectContent'></div></div>";
                //$(closingPrompt).fadeTo("fast", 0, function() {
                var strdiv1 = document.createElement('div');
                var strdiv = $(strdiv1);
                strdiv.attr("id", strID);
                strdiv.attr("class", "formError");
                strdiv.html("<div class='formCorrectContent'></div>");
                callerTopPosition = $(caller).offset().top;
                callerleftPosition = $(caller).offset().left;
                callerWidth = $(caller).width()
                inputHeight = $(strdiv).height()

                /* POSITIONNING */
                if ($.validationEngine.settings.promptPosition == "topRight") { callerleftPosition += callerWidth - 30; callerTopPosition += -inputHeight - 10; }
                if ($.validationEngine.settings.promptPosition == "topLeft") { callerTopPosition += -inputHeight - 10; }

                if ($.validationEngine.settings.promptPosition == "centerRight") { callerleftPosition += callerWidth + 13; }

                if ($.validationEngine.settings.promptPosition == "bottomLeft") {
                    callerHeight = $(caller).height();
                    callerleftPosition = callerleftPosition;
                    callerTopPosition = callerTopPosition + callerHeight + 15;
                }
                if ($.validationEngine.settings.promptPosition == "bottomRight") {
                    callerHeight = $(caller).height();
                    callerleftPosition += callerWidth - 30;
                    callerTopPosition += callerHeight + 15;
                }
                strdiv.css({
                    top: callerTopPosition,
                    left: callerleftPosition
                });
                $(closingPrompt).remove();
                $("#"+strID).remove();
                $("body").append(strdiv);

                //});

            }
        },
        debug: function(error) {
            if (!$("#debugMode")[0]) {
                $("body").append("<div id='debugMode'><div class='debugError'><strong>This is a debug mode, you got a problem with your form, it will try to help you, refresh when you think you nailed down the problem</strong></div></div>")
            }
            $(".debugError").append("<div class='debugerror'>" + error + "</div>")
        },
        submitValidation: function(caller, settings) {					// FORM SUBMIT VALIDATION LOOPING INLINE VALIDATION
            var stopForm = false;
            $.validationEngine.settings = settings
            $.validationEngine.ajaxValid = true
            $(caller).find(".formError").remove();
            var toValidateSize = $(caller).find("[class^=validate]").size();

            $(caller).find("[class^=validate]").each(function() {
                callerId = $(this).attr("id")
                if (!$("." + callerId + "formError").hasClass("ajaxed")) {	// DO NOT UPDATE ALREADY AJAXED FIELDS (only happen if no normal errors, don't worry)
                    var validationPass = $.validationEngine.loadValidation(this, settings);
                    return (validationPass) ? stopForm = true : "";
                }
            });
            ajaxErrorLength = $.validationEngine.ajaxValidArray.length		// LOOK IF SOME AJAX IS NOT VALIDATE
            for (x = 0; x < ajaxErrorLength; x++) {
                if ($.validationEngine.ajaxValidArray[x][1] == false) {
                    $.validationEngine.ajaxValid = false
                }
            }
            if (stopForm || !$.validationEngine.ajaxValid) {		// GET IF THERE IS AN ERROR OR NOT FROM THIS VALIDATION FUNCTIONS
                //                destination = $(".formError:not('.greenPopup'):first").offset().top;
                //                $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination }, 1100);
                return true;
            } else {
                return false
            }
        }
    }
})(jQuery);